package com.gitee.hifong45;

import java.util.LinkedHashMap;
import java.util.Map;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import lombok.var;

public enum Msg {
	// success
	SUCCESS(200, "success"),

	CANNOT_BE_NULL(10010, "can NOT be null"),
	ERROR_PARAMETER(10011, "error parameter"),
	DB_ERROR(10012, "database result failed"),
	
	NOT_FOUND(10014, "can NOT find target"),
	HAS_EXISTS(10015, "target has existed"),
	HAS_ONE(10016, "target has one"),
	HAS_MORE(10017, "target has more than one"),
	NOT_ONE(10018, "target is NOT one(0/more)"),
	NOT_ENOUGH(10019, "target is NOT enough)"),
	NEED_SESSION_KEY(10022, "can NOT find session-key"),
	
	ACCESS_FAILED(10091, "access failed"),
	TIMES_REACH_LIMIT(10092, "over the limit"),
	TIME_EXPIRED(10093, "has expired"),
	EXCEPTION(10099, "java excepition"),
	
	NEED_LOGIN(20001, "user need login"),
	LOGIN_FAILED(20002, "account/password are error"),
	ACCESS_DENIED(20003, "access has been denied"),
	USER_LOCKED_PWDERRORS(20101, "user locked because password-login failed more times"),
	USER_NOT_FOUND(20110, "can NOT find the user"),
	
	SEND_PHONECODE_FAILED(50011, "Send phone code failed!"),
	SEND_MAILCODE_FAILED(50012, "Send mail code failed!"),
	VERIFY_PHONE_FAILED(50013, "Phone code verify failed!"),
	VERIFY_MAIL_FAILED(50014, "Mail code verify failed!"),
	VERIFY_FAILED(50015, "Code verify failed!"),
	
	FRIEND_EXISTS(30002, "好友已存在"),
	
	// Pay
	NOT_GET_PAY_CONTENT(61001, "pay service can NOT get right qr-code content"),
	
	ZZZNOT_USED_LAST_ONE(9999); //"这个只是为了增减时不用处理最后的逗号/分号，实际中不使用"
	
    public int code;
    public String msg;

    Msg(int code) {
    	this.code = code;
    }

    Msg(int code, String msg) {
    	this.code = code;
    	this.msg = msg;
    }
    
    public int getCode() {
		return code;
	}

	/**
	 * 替换原有信息
	 * @param msg 新信息
	 * @return 新消息体
	 */
	public Msg setMsg(String msg) {
		this.msg = msg;
		return this;
	}

    public Map<String, Object> toMap(boolean... addTime) {
		Kv ret = Kv.of("code", this.code, "msg", this.msg);
		if(addTime.length == 1 && addTime[0]) {
			ret.put("tm", DateUtil.now());
		}
    	return ret;
    }

	public String toJStr(boolean... addTime) {
		return JSONUtil.toJsonStr(this.toMap(addTime));
	}

	public String toJStr(String extMsg, boolean... addTime) {
		return JSONUtil.toJsonStr(this.toMap(extMsg, addTime));
	}

	public Map<String, Object> toMap(String extMsg, boolean... addTime) {
		String m = this.msg;
		if(StrUtil.isNotBlank(extMsg)) {
			m  = m + " - "+ extMsg;
		}
		Kv ret = Kv.of("code", this.code, "msg", m);
		if(addTime.length == 1 && addTime[0]) {
			ret.put("tm", DateUtil.now());
		}
		return ret;
	}

    public static String export() {
    	var map = new LinkedHashMap<Integer, String>();
    	
    	for (var enu : Msg.values()) {
    		map.put(enu.code, enu.msg);
		}
    	var s = map.toString();
    	s = s.replace("=", ": '").replace(",", "',").replace("'null}", "null}");
    	Console.log(s);
		return s;
	}

	public static void main(String[] args) {
		Console.log(Msg.ERROR_PARAMETER.toJStr());
		Console.log(Msg.ERROR_PARAMETER.toMap("额外信息1"));
		Console.log(Msg.ERROR_PARAMETER.toJStr("额外信息2", true));
		Console.log(Msg.ERROR_PARAMETER.setMsg("这是直接环形操作的本体消息1").toMap());
		Console.log(Msg.ERROR_PARAMETER.setMsg("这是直接环形操作的本体消息2").toJStr());
	}

}
